/*                                  ATELIER                                   */

/* %%LICENSE_TAG%%                                                            */
package edu.gmu.view.canvas;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.event.MouseInputListener;

/*******************************************************************
 * Draw a vertical and horizontal line that intersects where the
 * mouse is currently located.
 *******************************************************************/
public final class CursorLayer extends AbstractLayer implements MouseInputListener
{
    /**
     *
     */
    public static final int CROSSHAIR  = 0;
    /**
     *
     */
    public static final int VERTICAL   = 1;
    /**
     *
     */
    public static final int HORIZONTAL = 2;
	
    /**
     *
     */
    protected int type = CROSSHAIR;
	
    /**
     *
     */
    protected Color color = null;
    
    /**
     *
     */
    protected Color readoutColor = null;
    
    /**
     *
     */
    protected Point currentPoint = null;
    
    /**
     *
     */
    protected boolean showReadout = true;
    
    /**
     *
     */
    protected CursorReadoutAdapter readoutAdapter = null;
    
    /**
     * Creates a new BackgroundScanLayer with default parameters.
     */
    public CursorLayer ( )
    {
        this( Color.GREEN );
    }
    
    /**
     * Creates a new CursorLayer with specified parameters.
     * 
     * 
     * @param color
     */
    public CursorLayer ( Color  color )
    {
        this( CROSSHAIR, color );
    }
    
    /**
     * Creates a new CursorLayer with specified parameters.
     * 
     * @param type 
     * @param color
     */
    public CursorLayer ( int type, Color  color )
    {
    	this.type = type;
        this.color = color;
        this.readoutColor = color;
    }
    

    //////////////////////////////////////////////////////////////////////
    // accessor methods
    //////////////////////////////////////////////////////////////////////

    /**
     *
     * @return
     */
    public int getType( )
    {
        return this.type;
    }
    
    /**
     *
     * @return
     */
    public Color getColor( )
    {
        return this.color;
    }
    
    /**
     *
     * @return
     */
    public Color getReadoutColor( )
    {
        return this.readoutColor;
    }
    
    /**
     *
     * @return
     */
    public boolean getShowReadout( )
    {
        return this.showReadout;
    }
    
    /**
     *
     * @return
     */
    public CursorReadoutAdapter getReadoutAdapter( )
    {
        return this.readoutAdapter;
    }

    //////////////////////////////////////////////////////////////////////
    // mutator methods
    //////////////////////////////////////////////////////////////////////

    /**
     *
     * @param type
     */
    public void setType( int type )
    {
        this.type = type;
    }
    
    /**
     *
     * @param color
     */
    public void setColor( Color  color )
    {
        this.color = color;
    }
    
    /**
     *
     * @param readoutColor
     */
    public void setReadoutColor( Color  readoutColor )
    {
        this.readoutColor = readoutColor;
    }
    
    /**
     *
     * @param showReadout
     */
    public void setShowReadout( boolean  showReadout )
    {
        this.showReadout = showReadout;
    }
    
    /**
     *
     * @param cursorReadoutAdapter
     */
    public void setReadoutAdapter( CursorReadoutAdapter cursorReadoutAdapter )
    {
        this.readoutAdapter = cursorReadoutAdapter;
    }
    
    /*********************************************************************
     * Paints images on specified Component object using passed Graphics.
     *
     * @param component
     * @param graphics
     *********************************************************************/
    public void paintLayer( JComponent  component, Graphics2D  graphics )
    {
        if( this.currentPoint == null )
        {
            return;
        }
        
        if ( color == null )
        {
            graphics.setColor ( component.getForeground() );
        }
        else
        {
            graphics.setColor ( color );
        }

        int width = component.getWidth();
        int height = component.getHeight();

        int x = this.currentPoint.x;
        int y = this.currentPoint.y;
        
        if( this.type == CROSSHAIR || this.type == VERTICAL )
        {
	        /*
	         * Draw a vertical line
	         */
            graphics.drawLine( x, 0, x, height );
        }
        
        if( this.type == CROSSHAIR || this.type == HORIZONTAL )
        {
	        /*
	         * Draw a horizontal line
	         */
	        graphics.drawLine( 0, y, width, y );
        }
        
        //------------------------------ Readout -----------------------------//
        if( this.showReadout == true )
        {
	        // Use readoutAdapter if specified
	        String readout = null;
	        if( this.readoutAdapter != null )
	        {
	        	readout = this.readoutAdapter.convertPosition(x, y, component);
	        }
	        else
	        {
	        	readout = x + ", " + y;
	        }
	        
	        if( readout != null )
	        {
	        	//System.out.println(mouseX+", "+mouseY);
		        int readoutWidth = graphics.getFontMetrics().stringWidth( readout ) + 18;
		        int readoutHeight = graphics.getFontMetrics().getHeight() + 5;
		        Rectangle readoutBounds = new Rectangle(x+5,y-15,readoutWidth,readoutHeight);
		        if(component.getSize().getWidth() <= readoutBounds.x+readoutBounds.width)
		        {
		            readoutBounds = new Rectangle(
		                x-60,y-15,readoutBounds.width,readoutBounds.height);
		        }
		        if(readoutBounds.y < 0)
		        {
		            readoutBounds = new Rectangle(
		                readoutBounds.x,y+10,readoutBounds.width,readoutBounds.height);
		        }
		        graphics.setColor(new Color(128,128,128,150));
		        //System.out.println(readoutBounds.toString());
		        //System.out.println(component.getSize().height);
		        graphics.fillRoundRect( readoutBounds.x,
		                                readoutBounds.y,
		                                readoutBounds.width,
		                                readoutBounds.height,
		                                10,
		                                10  );
		        graphics.setColor(readoutColor);

                graphics.drawString(
		                readout,
		                readoutBounds.x+12,
		                readoutBounds.y+14  );
	        }
        }
    }

    
    //------------------------- Delegated MouseEvents ------------------------//
    public void mouseClicked(MouseEvent e)
    {
        
    }

    public void mousePressed(MouseEvent e)
    {
        
    }

    public void mouseReleased(MouseEvent e)
    {
        
    }

    public void mouseEntered(MouseEvent e)
    {
        
    }

    public void mouseExited(MouseEvent e)
    {
        // Set our current point in order to erase the cursor
        currentPoint = null;
    }

    public void mouseDragged(MouseEvent e)
    {
        
    }

    public void mouseMoved(MouseEvent e)
    {
        // Set our current point in order to draw the cursor
        currentPoint = e.getPoint();
    }
     
}

/*                                  ATELIER                                   */